package com.itheima.reggie.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BusinessException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;


    //菜品分类列表
    @Override
    public R categoryList(Integer type) {
        //List<Category> list= categoryMapper.getCategoryListByType(type);
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(type!= null,Category::getType,type).orderByDesc();
        List<Category> list = categoryMapper.selectList(qw);
        return R.success(list);
    }

    //修改分类
    @Override
    public R updateCategory(Category category) {
        try {
            categoryMapper.updateById(category);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("修改失败");
        }
        return R.success("修改成功");
    }



    //删除分类
    @Override
    public R deleteCategory(Long id) {
        //使用mp
        //查询当前分类下是否有菜品
        Integer dishCount = categoryMapper.selectCountForDish(id);
        if (dishCount > 0) {
            throw new BusinessException("当前分类下任有菜品,无法删除");
        }
        //查询当前分类下是否有套餐
        Integer setmealCount = categoryMapper.selectCountForSetmeal(id);
        if (setmealCount > 0) {
            throw new BusinessException("当前分类下任有套餐,无法删除");
        }
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getId, id);
        //----------------------使用mp
        categoryMapper.delete(qw);
        //----------------------mybatis
       // categoryMapper.deleteCategoryById(id);
        return R.success("删除成功");
    }


    //分类分页
    @Override
    public R categoryPage(Integer page, Integer pageSize) {
        //使用mp
        Page<Category> iPage = new Page<>();
        categoryMapper.selectPage(iPage, null);
        //获取数据
        List<Category> records = iPage.getRecords();
        //获取总数
        long total = iPage.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        return R.success(map);
    }

    //新增分类
    @Override
    public R addCategory(Category category, HttpSession session) {
        try {
            //补全信息
            //补全修改人，创建人，修改时间，创建时间
 /*           Long employee = (Long) session.getAttribute("employee");
            category.setUpdateUser(employee);
            category.setCreateUser(employee);
            category.setUpdateTime(LocalDateTime.now());
            category.setCreateTime(LocalDateTime.now());*/
            //--------------------------------使用mp
            categoryMapper.insert(category);
            //-------------------------------使用mybatis
            //调用mybatis新增员工
            //用hutool创建雪花算法
 /*           Snowflake snowflake = IdUtil.getSnowflake(1, 1);
            long id = snowflake.nextId();
            category.setId(id);
            categoryMapper.addCategory(category);*/
            return R.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("添加失败");
        }
    }


}
